package com.yy.common.base;


import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.yy.common.R;
import com.yy.common.baseapp.AppManager;
import com.yy.common.baserx.RxManager;
import com.yy.common.commonutils.TUtil;
import com.yy.common.commonutils.ToastUitl;
import com.yy.common.commonwidget.LoadingDialog;
import com.yy.common.commonwidget.NormalTitleBar;
import com.yy.common.commonwidget.StatusBarCompat;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Activity基类
 *
 * @param <T>
 */
public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity {
    public String TAG = "";
    public T mPresenter;
//    public E mModel;
    public Context mContext;
    public RxManager mRxManager;
    private NormalTitleBar normalTitleBar;
    private FrameLayout contentFrame;
    private Unbinder unbinder;
    View.OnClickListener backOnClickListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        //默认隐藏键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mRxManager = new RxManager();
        doBeforeSetcontentView();
        setContentView(R.layout.act_base_main);
        normalTitleBar = (NormalTitleBar) findViewById(R.id.ntb);
        contentFrame = (FrameLayout) findViewById(R.id.content_frame);
        contentFrame.addView(LayoutInflater.from(this).inflate(getLayoutId(), null));
        unbinder = ButterKnife.bind(this);
        mContext = this;
        mPresenter = TUtil.getT(this, 0);
//        mModel = TUtil.getT(this, 1);
        if (mPresenter != null) {
            mPresenter.mContext = this;
            mPresenter.setVM(this);
        }
        initBaseView();
        this.initView();
    }

    /**
     * 初始化BaseActivity基类View
     */
    private void initBaseView() {
        normalTitleBar.setOnBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    /**
     * 设置layout前配置
     */
    private void doBeforeSetcontentView() {
        // 把actvity放到application栈中管理
        AppManager.getAppManager().addActivity(this);
        // 无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 默认着色状态栏
        SetStatusBarColor(getResources().getColor(R.color.main_blue));

    }

    /*********************
     * 子类实现
     *****************************/
    //获取布局文件
    public abstract int getLayoutId();

    //简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
//    public abstract void initPresenter();
    //初始化view
    public abstract void initView();

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor() {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.white));
    }

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            StatusBarCompat.setStatusBarColor(this, color);
        }
    }

    /**
     * 沉浸状态栏（4.4以上系统有效）
     */
    protected void SetTranslanteBar() {
        StatusBarCompat.translucentStatusBar(this);
    }


    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 开启浮动加载进度条
     */
    public void startProgressDialog() {
        LoadingDialog.showDialogForLoading(this);
    }

    /**
     * 开启浮动加载进度条
     *
     * @param msg
     */
    public void startProgressDialog(String msg) {
        LoadingDialog.showDialogForLoading(this, msg, true);
    }

    /**
     * 停止浮动加载进度条
     */
    public void stopProgressDialog() {
        LoadingDialog.cancelDialogForLoading();
    }

    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        ToastUitl.showShort(text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        ToastUitl.showShort(resId);
    }

    /**
     * 长时间显示Toast提示(来自res)
     **/
    public void showLongToast(int resId) {
        ToastUitl.showLong(resId);
    }

    /**
     * 长时间显示Toast提示(来自String)
     **/
    public void showLongToast(String text) {
        ToastUitl.showLong(text);
    }

    /**
     * 带图片的toast
     *
     * @param text
     * @param res
     */
    public void showToastWithImg(String text, int res) {
        ToastUitl.showToastWithImg(text, res);
    }

    /**
     * 网络访问错误提醒
     */
    public void showNetErrorTip() {
        ToastUitl.showToastWithImg(getText(R.string.net_error).toString(), R.drawable.ic_wifi_off);
    }

    public void showNetErrorTip(String error) {
        ToastUitl.showToastWithImg(error, R.drawable.ic_wifi_off);
    }

    /**
     * 显示标题栏
     *
     * @param title
     */
    public void showTitle(String title) {
        normalTitleBar.setVisibility(View.VISIBLE);
        normalTitleBar.setTitleText(title);
        normalTitleBar.showTitleView(true);
        normalTitleBar.setTvLeftVisiable(false);
    }
    /**
     * 显示/隐藏标题文字
     */
    public void hideTitleView() {
        normalTitleBar.setVisibility(View.GONE);
    }

    /**
     * 显示/隐藏右标题文字
     */
    public void showRightTitleView(boolean isShow) {
        normalTitleBar.setRightTitleVisibility(isShow);
    }

    /**
     * 显示左边图片
     * @param resId
     * @param listener
     */
    public void showLeftImageView(int resId,View.OnClickListener listener) {
        normalTitleBar.setOnLeftImagListener(resId,listener);
    }
    public void showUnreadImageView(int resId,boolean isShow) {
        normalTitleBar.showUnreadImageView(resId,isShow);
    }

    /**
     * 显示右边图片
     * @param resId
     * @param listener
     */
    public void showRightImageView(int resId,View.OnClickListener listener) {
        normalTitleBar.setOnRightImagListener(resId,listener);
    }

    /**
     * 显示右边图片
     * @param resId
     */
    public void showRightImageView(int resId) {
        normalTitleBar.setOnRightImagListener(resId);
    }
    /**
     * 显示标题栏&&监听返回按钮事件（backOnClickListener传null则默认返回上一个界面）
     *
     * @param title
     * @param backOnClickListener
     */
    public void showTitle(String title, View.OnClickListener backOnClickListener) {
        this.backOnClickListener = backOnClickListener;
        normalTitleBar.setVisibility(View.VISIBLE);
        normalTitleBar.setTitleText(title);
        normalTitleBar.setTvLeftVisiable(true);
        if (backOnClickListener != null) normalTitleBar.setOnBackListener(backOnClickListener);
    }

    /**
     * 显示标题栏&&监听返回按钮事件（backOnClickListener传null则默认返回上一个界面）
     *
     * @param title
     */
    public void setRightText(String title) {
        normalTitleBar.setRightTitle(title);
    }

    /**
     * 显示右边文字
     *
     * @param title
     * @param backOnClickListener
     */
    public void showRightBtn(String title, View.OnClickListener backOnClickListener) {
        normalTitleBar.setVisibility(View.VISIBLE);
        normalTitleBar.setRightTitle(title);
        if (backOnClickListener != null) normalTitleBar.setOnRightTextListener(backOnClickListener);
    }

    /**
     * 显示右边文字
     *
     * @param title
     * @param backOnClickListener
     */
    public void showSpecialRightBtn(String title, View.OnClickListener backOnClickListener) {
        showSpecialRightBtn(true);
        normalTitleBar.setSpecialRightTitle(title);
        if (backOnClickListener != null) normalTitleBar.setOnSpecialRightTextListener(backOnClickListener);
    }

    /**
     * 显示右边文字
     *
     * @param isShow
     */
    public void showSpecialRightBtn(boolean isShow) {
        normalTitleBar.setVisibility(View.VISIBLE);
        normalTitleBar.showSpecialRightTitle(isShow);
    }

    /**
     * 右边特殊文字是否可点击
     *
     * @param isClick
     */
    public void showRightClick(boolean isClick) {
        normalTitleBar.setVisibility(View.VISIBLE);
        normalTitleBar.setSpecialRightTitleClick(isClick);
    }
    @Override
    protected void onResume() {
        super.onResume();
            //友盟统计
            MobclickAgent.onResume(this);
            MobclickAgent.onPageStart(TAG);
    }

    @Override
    protected void onPause() {
        super.onPause();
            //友盟统计
            MobclickAgent.onPause(this);
            MobclickAgent.onPageEnd(TAG);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (backOnClickListener != null){
                backOnClickListener.onClick(null);
//                return false;
            }
            return super.onKeyDown(keyCode, event);
        }else {
            return super.onKeyDown(keyCode, event);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.onDestroy();
        unbinder.unbind();
        mRxManager.clear();
        AppManager.getAppManager().finishActivity(this);
    }
}
